package com.restkeeper.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.restkeeper.entity.SystemConfig;
import com.restkeeper.mapper.SystemConfigMapper;
import com.restkeeper.utils.Result;
import com.restkeeper.utils.ResultCode;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.CollectionUtils;

import java.util.List;

@RequiredArgsConstructor
@Service
@RefreshScope
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService{

    private final SystemConfigMapper systemConfigMapper;
    private final CommonService commonService;
    private final RedisTemplate redisTemplate;



    @Override
    public Result getAppDownloadLink() {
        QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("sys_key","appDownloadLink");
        queryWrapper.last("limit 1");
        SystemConfig systemConfig = systemConfigMapper.selectOne(queryWrapper);
        if(systemConfig==null || StringUtils.isBlank(systemConfig.getSysValue())){
            return Result.error(ResultCode.config_empty,"链接信息缺失");
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("url",systemConfig.getSysValue());
        return Result.ok(jsonObject);
    }

    @Override
    public Result versionInfo() {
        QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("sys_type","versionInfo");
        queryWrapper.last("limit 1");
        SystemConfig systemConfig = systemConfigMapper.selectOne(queryWrapper);
        /**
         * versionName=sysKey=版本号
         * versionCode=sysValue=code
         * link=url=app下载链接
         * */
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("versionName","");
        jsonObject.put("versionCode","");
        jsonObject.put("link","");
        if(systemConfig!=null){
            jsonObject.put("versionName",systemConfig.getSysKey());
            jsonObject.put("versionCode",systemConfig.getSysValue());
            jsonObject.put("link",systemConfig.getUrl());
        }
        return Result.ok(jsonObject);
    }

    @Override
    public Result getVideoFlag() {
        return Result.ok(systemConfigMapper.selectOneByKey("video_flag"));
    }



    @Override
    public Result shortUnlock() {
        SystemConfig systemConfig = systemConfigMapper.selectOneByKey("short_unlock");
        if(systemConfig==null){
            return Result.ok(null);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",systemConfig.getId());
        jsonObject.put("sysValue",Integer.valueOf(systemConfig.getSysValue()));
        jsonObject.put("sysTitle",systemConfig.getSysTitle());
        return Result.ok(jsonObject);
    }



}
